package com.pk8;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {

    public static void main(String[] args) {
        StudentService studentService = new StudentService();
        List<StudentVo> studentVoList = studentService.initStudentList();
        System.out.println(studentService.getAverageAge(studentVoList));
        StudentVo minAgeStudentVo = studentService.getMinAgeStudentVo(studentVoList);
        System.out.println(minAgeStudentVo);
        List<StudentVoSchool> studentVoSchools = studentService.mapToStudentVo(studentVoList);
        studentVoSchools.add(new StudentVoSchool("zhangsan",12,"university"));
        System.out.println(studentVoSchools);

    }

}

class StudentService {
    public List<StudentVo> initStudentList() {
        List<StudentVo> studentVoList = new ArrayList<>();
        studentVoList.add(new StudentVo("张三", 18));
        studentVoList.add(new StudentVo("李四", 19));
        studentVoList.add(new StudentVo("王五", 8));
        studentVoList.add(new StudentVo("lily", 10));
        studentVoList.add(new StudentVo("david", 20));
        return studentVoList;
    }

    public int getAverageAge(List<StudentVo> studentVoList) {
        OptionalDouble average = studentVoList.stream().mapToInt(x -> x.getAge()).average();
        return (int) average.orElse(0);
    }

    public StudentVo getMinAgeStudentVo(List<StudentVo> studentVoList) {
//        studentVoList.stream().min(new Comparator<StudentVo>() {
//            @Override
//            public int compare(StudentVo o1, StudentVo o2) {
//                return o1.getAge()-o1.getAge();
//            }
//        })
        Optional<StudentVo> min = studentVoList.stream().min((StudentVo o1, StudentVo o2) -> o1.getAge() - o2.getAge());
        return min.orElse(null);

    }

    public List<StudentVoSchool> mapToStudentVo(List<StudentVo> studentVoList) {
        Stream<StudentVoSchool> studentVoSchoolStream = studentVoList.stream().map(x -> new StudentVoSchool(x.getName(), x.getAge(), "my school"));
        List<StudentVoSchool> collect = studentVoSchoolStream.collect(Collectors.toList());
        return collect;
    }
}

class StudentVo {
    public StudentVo(String name, int age) {
        this.name = name;
        this.age = age;
    }

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "StudentVo{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class StudentVoSchool {
    private String name;
    private int age;
    private String school;

    public StudentVoSchool(String name, int age, String school) {
        this.name = name;
        this.age = age;
        this.school = school;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSchool() {
        return school;
    }

    public void setSchool(String school) {
        this.school = school;
    }

    @Override
    public String toString() {
        return "StudentVoSchool{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", school='" + school + '\'' +
                '}';
    }
}